package com.gainloop.Listeners;

import java.util.ArrayList;

import org.alexd.jsonrpc.JSONRPCException;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.facebook.android.Facebook;
import com.gainloop.GainPage;
import com.gainloop.Login;
import com.gainloop.Adapters.HelpAdapter;
import com.gainloop.Api.Helps;
import com.gainloop.Api.Entity.Help;

public class HelpResolvedButtonListener implements View.OnClickListener{
	ArrayList<Help> _currentHelps;
	Help _currentHelp;
	HelpAdapter _helpAdapter;
	Context _context;
	
	public HelpResolvedButtonListener(Context context,ArrayList<Help> currentHelps,  HelpAdapter helpAdapter,Help currentHelp){
		Log.v("Tag Resolved Button Listener","created");
		_currentHelps=currentHelps;
		_helpAdapter = helpAdapter;
		_currentHelp=currentHelp;
		_context=context;
	}
	
	@Override
	public void onClick(View view) {
		Log.v("Help Resolved Button Listener","clicked!");
		//_currentHelps.remove(_currentHelp);
		try {
			Helps.getInstance().setHelpResolved(_currentHelp);
		} catch (JSONRPCException e) {
			e.printStackTrace();
			throw new RuntimeException("Error resolving help");
		}
		//postOnWall("checking android app");
		//sendEmail();
		Toast.makeText(_context, "This wish has been resolved!", Toast.LENGTH_LONG).show();
		_helpAdapter.notifyDataSetChanged();
	}
	private void sendEmail(){
		Intent i = new Intent(Intent.ACTION_SEND);
		i.setType("text/plain");
		i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"yotamsha@gmail.com"});
		i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
		i.putExtra(Intent.EXTRA_TEXT   , "body of email");
		try {
		    _context.startActivity(Intent.createChooser(i, "Send mail..."));
		} catch (android.content.ActivityNotFoundException ex) {
		    Toast.makeText(_context, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
		}
	}
	private void postOnWall(String msg) {
        Log.d("Tests", "Testing graph API wall post");
         try {
        	 Facebook facebook = new Facebook(Login.APP_ID);
                String response = facebook.request("me");
                Bundle parameters = new Bundle();
                parameters.putByteArray("message", msg.getBytes());
                parameters.putByteArray("description", "test test test".getBytes());
                response = facebook.request("me/feed", parameters, 
                        "POST");
                Log.d("Tests", "got response: " + response);
                if (response == null || response.equals("") || 
                        response.equals("false")) {
                   Log.v("Error", "Blank response");
                }
         } catch(Exception e) {
             e.printStackTrace();
         }
    }
}
